package com.koron.plan.web.patrolplan.service.impl;

import cn.hutool.core.lang.Assert;
import com.koron.common.core.util.CodeTools;
import com.koron.common.core.util.StringUtils;
import com.koron.plan.web.common.CommonUtils;
import com.koron.plan.web.patrolplan.bean.entity.PatrolPlanWorkerBean;
import com.koron.plan.web.patrolplan.mapper.PatrolPlanWorkerMapper;
import com.koron.plan.web.patrolplan.service.PatrolPlanWorkerService;
import org.apache.commons.collections4.CollectionUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @author twcao
 * @title: PatrolPlanWorkerServiceImpl
 * @projectName GDH_EAM_V2
 * @description: 巡视人员serviceImPL
 * @date 2021/10/2714:04
 */
@Service
public class PatrolPlanWorkerServiceImpl implements PatrolPlanWorkerService {
    /**
     * 通过ID查询单条数据
     *
     * @param factory
     * @param id      主键
     * @return 实例对象
     */
    @Override
    public PatrolPlanWorkerBean queryById(SessionFactory factory, String id) {
        return null;
    }

    /**
     * 新增/编辑数据
     *
     * @param factory
     * @param bean    实例对象
     * @return 实例对象
     */
    @Override
    public PatrolPlanWorkerBean saveOrUpdate(SessionFactory factory, PatrolPlanWorkerBean bean) {
        Assert.notNull(bean, "巡视人员不能为空");
        List<PatrolPlanWorkerBean> beans = saveOrUpdate(factory, Arrays.asList(bean));
        return beans.get(0);
    }

    /**
     * 新增/编辑数据
     *
     * @param factory
     * @param beans   实例对象
     * @return 实例对象
     */
    @Override
    public List<PatrolPlanWorkerBean> saveOrUpdate(SessionFactory factory, List<PatrolPlanWorkerBean> beans) {
        if(CollectionUtils.isEmpty(beans)) {
            return beans;
        }
        PatrolPlanWorkerMapper mapper = factory.getMapper(PatrolPlanWorkerMapper.class);
        CommonUtils.sorted(beans);
        CommonUtils.update(beans);
        beans.stream().forEach(worker -> worker.setId(StringUtils.isBlank(worker.getId()) ? CodeTools.getCode32() : worker.getId()));
        mapper.saveOrUpdate(beans);
        return beans;
    }

    /**
     * 通过主键删除数据
     *
     * @param factory
     * @param id      主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(SessionFactory factory, String id) {
        return false;
    }

    @Override
    public boolean deleteByPlanId(SessionFactory factory, String planId) {
        PatrolPlanWorkerMapper mapper = factory.getMapper(PatrolPlanWorkerMapper.class);
        return mapper.deleteByPlanId(planId) > 0;
    }

    @Override
    public boolean hiddenByPlanId(SessionFactory factory, String planId) {
        PatrolPlanWorkerMapper mapper = factory.getMapper(PatrolPlanWorkerMapper.class);
        return mapper.hiddenByPlanId(planId) > 0;
    }
}
